<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">

    <ui:composition template="layout.xhtml">
        <ui:define name="title">
            <h2><a href="#" class="back" onclick="history.go(-1)"/>Cadastrar Protocolo</h2>
        </ui:define>

        <ui:define name="body">

            <h:form id="protocoloForm" enctype="multipart/form-data">            
                <p:messages id="messages" closable="true" showDetail="false" autoUpdate="true"  />
                <p:panel id="protocoloPanel" styleClass="container_24 clearfix" >
                    <p:outputLabel value="Numero:" styleClass="grid_3" 
                                   rendered="#{managerLayout.gerarNumeroAutomatico}" for="numeroInputText"/>
                    <p:inputText id="numeroInputText" styleClass="grid_8" 
                                 required="true" maxlength="9" rendered="#{managerLayout.gerarNumeroAutomatico}"
                                 onkeypress="jQuery(this).autoNumeric({aSign:'', aSep: null, aDec: null, aPad: false} );"
                                 value="#{managerCriarProtocolo.protocolo.numero}">
                        <f:convertNumber integerOnly="true" />
                    </p:inputText>

                    <p:outputLabel value="Data:" styleClass="grid_3" for="dataCalendar" />
                    <p:calendar id="dataCalendar" value="#{managerCriarProtocolo.protocolo.dataProtocolo}"
                                showOn="both" locale="pt" yearRange="1900:2100" 
                                navigator="true" pattern="dd/MM/yyyy" styleClass="grid_9" />
                    <div class="clear"/>
                    <br/>
                </p:panel>

                <h2>Credor</h2>

                <p:panel id="clientePanel" styleClass="container_24 clearfix">
                    <p:outputLabel id="personOutputLabel" value="Pessoa:" 
                                   for="personRadioButton"
                                   styleClass="grid_3" style="top: 6px" />
                    <div class="grid_8 divSelectOneMenu">
                        <p:selectOneRadio id="personRadioButton" value="#{managerCriarProtocolo.cliente.tipoDocumento}" >
                            <f:selectItems value="#{managerCriarProtocolo.tipoDocumento}" />
                            <p:ajax event="change" update=":protocoloForm:clientePanel, :protocoloForm:messages" />
                        </p:selectOneRadio>
                    </div>
                    <div class="clear"/>

                    <p:outputLabel value="#{managerCriarProtocolo.pessoaFisica ? 'CPF:' : 'CNPJ:'}" 
                                   styleClass="grid_3" for="cpfCnpjInputMask" >
                        <span class="ui-outputlabel-rfi required">*</span>
                    </p:outputLabel>
                    <p:inputMask id="cpfCnpjInputMask"
                                 value="#{managerCriarProtocolo.pessoaFisica ? managerCriarProtocolo.cliente.cpf : managerCriarProtocolo.cliente.cnpj}" 
                                 styleClass="grid_8" 
                                 mask="#{managerCriarProtocolo.pessoaFisica ? '999.999.999-99' : '99.999.999/9999-99' }"
                                 autocomplete="false" >
                        <p:ajax event="blur" update=":protocoloForm:clientePanel, :protocoloForm:messages"
                                listener="#{managerCriarProtocolo.existeCliente()}" />
                    </p:inputMask>
                    <div class="clear"/>

                    <p:outputLabel value="#{managerCriarProtocolo.pessoaFisica ? 'Nome:' : 'Nome Fantasia:'}" 
                                   styleClass="grid_3 #{managerCriarProtocolo.pessoaFisica ? '' : 'nomeFantasiaLabel'}" 
                                   for="nomeInputText">
                        <span class="ui-outputlabel-rfi required">*</span>
                    </p:outputLabel>
                    <p:inputText id="nomeInputText" readonly="#{managerCriarProtocolo.cliente.id ne null}"
                                 value="#{managerCriarProtocolo.pessoaFisica ? managerCriarProtocolo.cliente.nome : managerCriarProtocolo.cliente.nomeFantasia}" 
                                 styleClass="grid_20" />
                    <div class="#{managerCriarProtocolo.pessoaFisica ? 'clear' : ''}" />

                    <p:outputLabel value="Nome da Mãe:" styleClass="grid_3" 
                                   for="nomeMaeInputText" rendered="#{managerCriarProtocolo.pessoaFisica}"/>
                    <p:inputText id="nomeMaeInputText" 
                                 value="#{managerCriarProtocolo.cliente.nomeMae}" 
                                 readonly="#{managerCriarProtocolo.cliente.id ne null}"
                                 rendered="#{managerCriarProtocolo.pessoaFisica}"
                                 styleClass="grid_20" />
                    <div class="#{managerCriarProtocolo.pessoaFisica ? 'clear' : ''}" />

                    <p:outputLabel value="RG:" styleClass="grid_3" for="rgInputText"
                                   rendered="#{managerCriarProtocolo.pessoaFisica}"/> 
                    <p:inputText id="rgInputText" styleClass="grid_8" 
                                 value="#{managerCriarProtocolo.cliente.rg}"
                                 readonly="#{managerCriarProtocolo.cliente.id ne null}"
                                 rendered="#{managerCriarProtocolo.pessoaFisica}"/>

                    <p:outputLabel value="Data de Nascimento:" styleClass="grid_4" 
                                   for="dataNascCalendar" rendered="#{managerCriarProtocolo.pessoaFisica}"/>
                    <p:calendar id="dataNascCalendar" value="#{managerCriarProtocolo.cliente.dataNascimento}"
                                showOn="both" locale="pt" yearRange="1800:c" 
                                readonly="#{managerCriarProtocolo.cliente.id ne null}"
                                rendered="#{managerCriarProtocolo.pessoaFisica and managerCriarProtocolo.cliente.id eq null}"
                                navigator="true" pattern="dd/MM/yyyy" styleClass="grid_9" />
                    <div class="grid_9">
                        <p:inputText id="dataNascInputText" value="#{managerCriarProtocolo.cliente.dataNascimento}"
                                     style="width: 90%" readonly="true"
                                     rendered="#{managerCriarProtocolo.pessoaFisica and managerCriarProtocolo.cliente.id ne null}">
                            <f:convertDateTime pattern="dd/MM/yyyy"/>
                        </p:inputText>
                    </div>
                </p:panel>

                <br/>
                <h2>Processo</h2>

                <p:panel id="processoCamposPanel" styleClass="container_24 clearfix" >
                    <p:outputLabel value="Peças do Processo:" styleClass="grid_3 tipoProcessoLabel" 
                                   for="tipoProcessoSelectOneMenu" />
                    <div class="grid_7">
                        <p:selectOneMenu id="tipoProcessoSelectOneMenu" panelStyle="width:150px;" 
                                         value="#{managerCriarProtocolo.processo.tipoProcesso}">
                            <f:selectItems value="#{managerCriarProtocolo.tipoProcesso}"/>
                            <p:ajax update=":protocoloForm:processoCamposPanel" event="change" oncomplete="required(false)"/>
                        </p:selectOneMenu>
                    </div>
                    <div class="clear" />

                    <p:outputLabel value="Data:" styleClass="grid_3" for="dataProcessoCalendar" />
                    <p:calendar id="dataProcessoCalendar" value="#{managerCriarProtocolo.processo.dataProcesso}"
                                showOn="both" locale="pt" yearRange="1900:2100" 
                                navigator="true" pattern="dd/MM/yyyy" styleClass="grid_8" />

                    <div class="#{managerCriarProtocolo.processoRequerimento or managerCriarProtocolo.processoReciboPagamento ? 'clear' : ''}" />
                    <p:outputLabel id="objetoLabel" value="Objeto:" for="objetoTextarea" 
                                   styleClass="grid_3"
                                   rendered="#{managerCriarProtocolo.processoRequerimento or managerCriarProtocolo.processoReciboPagamento}" >
                        <span class="ui-outputlabel-rfi required">*</span>
                    </p:outputLabel>
                    <p:inputTextarea id="objetoTextarea" styleClass="grid_20"
                                     rendered="#{managerCriarProtocolo.processoRequerimento or managerCriarProtocolo.processoReciboPagamento}"
                                     value="#{managerCriarProtocolo.processo.objeto}" />

                    <p:outputLabel value="Nº Serviço:" 
                                   styleClass="grid_3 numeroServicoLabel" 
                                   for="numeroServicoInputText"
                                   rendered="#{managerCriarProtocolo.processoAutorizacao}">
                        <span class="ui-outputlabel-rfi required">*</span>
                    </p:outputLabel>
                    <p:inputText id="numeroServicoInputText" styleClass="grid_7" 
                                 value="#{managerCriarProtocolo.processo.numeroServico}"
                                 rendered="#{managerCriarProtocolo.processoAutorizacao}"/>
                    <div class="#{managerCriarProtocolo.processoAutorizacao ? 'clear' : ''}" />

                    <p:outputLabel value="Nº Processo Administrativo:" 
                                   styleClass="grid_3 numeroProcessoAdministrativoLabel" 
                                   for="numeroProcessoAdministrativoInputText"
                                   rendered="#{managerCriarProtocolo.processoAutorizacao}">
                        <span class="ui-outputlabel-rfi required">*</span>
                    </p:outputLabel>
                    <p:inputText id="numeroProcessoAdministrativoInputText" styleClass="grid_7"
                                 rendered="#{managerCriarProtocolo.processoAutorizacao}"
                                 value="#{managerCriarProtocolo.processo.numeroRequisicao}" />


                    <p:outputLabel value="Nº Requisição:" 
                                   styleClass="grid_3 numeroRequisicaoLabel" 
                                   for="numeroRequisicaoInputText"
                                   rendered="#{managerCriarProtocolo.processoAutorizacao}">
                        <span class="ui-outputlabel-rfi required">*</span>
                    </p:outputLabel>
                    <p:inputText id="numeroRequisicaoInputText" styleClass="grid_7"
                                 rendered="#{managerCriarProtocolo.processoAutorizacao}"
                                 value="#{managerCriarProtocolo.processo.numeroRequisicao}" />

                    <p:outputLabel value="Nota de Empenho:" 
                                   styleClass="grid_3 notaEmpenhoLabel" 
                                   rendered="#{managerCriarProtocolo.processoEmpenho}"
                                   for="notaEmpenhoInputText">
                        <span class="ui-outputlabel-rfi required">*</span>
                    </p:outputLabel>
                    <p:inputText id="notaEmpenhoInputText" styleClass="grid_7" 
                                 rendered="#{managerCriarProtocolo.processoEmpenho}"
                                 value="#{managerCriarProtocolo.processo.numeroNotaEmpenho}" />

                    <p:outputLabel value="Valor:" styleClass="grid_3" 
                                   rendered="#{managerCriarProtocolo.processoNotaLiquidacao or managerCriarProtocolo.processoPagamento}"
                                   for="valorInputText" >
                        <span class="ui-outputlabel-rfi required">*</span>
                    </p:outputLabel>
                    <p:inputText id="valorInputText" styleClass="grid_7" 
                                 onkeypress="jQuery(this).autoNumeric({aSign:'R$ ', aSep: '.', aDec: ','} );"
                                 rendered="#{managerCriarProtocolo.processoNotaLiquidacao or managerCriarProtocolo.processoPagamento}"
                                 value="#{managerCriarProtocolo.processo.valorLiquidacao}" >
                        <f:convertNumber type="currency" locale="pt,BR"/>
                    </p:inputText>

                    <div class="clear"/>
                    <br/>

                    <h2>Anexos</h2>

                    <p:fileUpload 
                        mode="advanced"
                        label="Anexar"
                        uploadLabel="Enviar"
                        cancelLabel="Cancelar"
                        fileUploadListener="#{managerCriarProtocolo.handleAnnex}"
                        multiple="true"
                        auto="true"
                        allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
                        dragDropSupport="true"
                        invalidFileMessage="Tipo de arquivo não suportado."
                        invalidSizeMessage="Tamanho do arquivo não é suportado"
                        immediate="true"
                        update=":protocoloForm:anexosPanel" />

                    <p:panel id="anexosPanel" styleClass="container_24 clearfix arquivosAnexadosPanel" header="Arquivos anexados">

                        <ui:repeat value="#{managerCriarProtocolo.processo.anexos}" var="a">
                            <p:panel styleClass="grid_5">
                                <h:outputLink  
                                    value="#{a.tipo ne 'application/pdf' ? 'viewImage.xhtml':'show'}" title="#{a.tipo}"  target="_blank" >

                                    <div style="width: 100%;" class="grid_4">

                                        <p:graphicImage id="anexoImagem"
                                                        styleClass="image grid_3" rendered="#{a.tipo ne 'application/pdf'}"
                                                        style="margin-left:3%; width: 80px;"
                                                        url="show/?annex=#{a.url.concat('thumbnail.').concat(a.nome)}" />

                                        <p:graphicImage id="anexoPdf"
                                                        styleClass="image grid_3" rendered="#{a.tipo eq 'application/pdf'}"
                                                        style="margin-left:3%; width: 80px;" 
                                                        url="media/images/pdf.jpeg" />

                                        <div class="clear"/>
                                        <p:outputLabel  value="#{managerCriarProtocolo.nomeAnexo(a)}" 
                                                        for="anexoImagem"
                                                        style="margin-left:1%; width: 100px;" rendered="#{a.tipo ne 'application/pdf'}"/>

                                        <p:outputLabel for="anexoPdf"
                                                       value="#{managerCriarProtocolo.nomeAnexoPdf(a)}" 
                                                       style="margin-left:1%; width: 100px;" rendered="#{a.tipo eq 'application/pdf'}"/>
                                    </div>
                                    <f:param name="annex" value="#{a.url.concat(a.nome)}"/>
                                </h:outputLink>

                                <p:commandButton id="removeButton" alt="Remover" value="Remover"
                                                 icon="ui-icon-trash" update=":protocoloForm:processoCamposPanel"
                                                 actionListener="#{managerCriarProtocolo.removerAnexo(a)}" />
                            </p:panel>
                        </ui:repeat>
                    </p:panel>
                    <hr style="margin-top: -10px"/>
                    <div class="clear" />
                    <div class="buttonAction">
                        <p:commandButton value="Adicionar"
                                         process=":protocoloForm:processoCamposPanel, @this"
                                         actionListener="#{managerCriarProtocolo.adicionarProcesso()}"
                                         update=":protocoloForm:processoCamposPanel, 
                                         :protocoloForm:processosListPanel"
                                         id="adicionarProcessoCommandButton" icon="ui-icon-plus"/>
                    </div>
                </p:panel>

                <p:panel id="processosListPanel" styleClass="container_24 clearfix">

                    <p:dataTable id="processosListTable" var="item" value="#{managerCriarProtocolo.processos}" 
                                 styleClass="grid_24" emptyMessage="Nenhum registro">  
                        <f:facet name="header">
                            Processos
                        </f:facet>

                        <p:column headerText="Ação" width="50" style="vertical-align: middle">
                            <p:commandButton  styleClass="optionButton" alt="Editar" 
                                              update=":protocoloForm:processoCamposPanel, :protocoloForm:processosListPanel"
                                              process="@this" icon="ui-icon-pencil"
                                              actionListener="#{managerCriarProtocolo.editarProcesso(item)}" />
                            <p:commandButton  styleClass="optionButton" alt="Remover"
                                              update=":protocoloForm:processosListPanel"
                                              process="@this" icon="ui-icon-trash"
                                              actionListener="#{managerCriarProtocolo.removerProcesso(item)}" />
                        </p:column>

                        <p:column headerText="Nome" style="vertical-align: middle">
                            #{item.tipoProcesso.nome}
                        </p:column>

                        <p:column headerText="Data" width="130" style="text-align: center; vertical-align: middle">  
                            <h:outputLabel value="#{item.dataProcesso}">
                                <f:convertDateTime pattern="dd/MM/yyyy" />
                            </h:outputLabel>
                        </p:column> 

                        <p:column headerText="Anexos" style="text-align: center;">  
                            <ui:repeat value="#{item.anexos}" var="a" >
                                <p:panel styleClass="grid_5" >
                                    <h:outputLink value="#{a.tipo ne 'application/pdf' ? 'viewImage.xhtml':'show'}" 
                                                  title="#{a.tipo}"  target="_blank" style="text-align: center">

                                        <div id="divLink" class="grid_4">

                                            <p:graphicImage styleClass="image grid_3" 
                                                            rendered="#{a.tipo ne 'application/pdf'}"
                                                            style="margin-left:1%; width: 40px;"
                                                            url="show/?annex=#{a.url.concat('thumbnail.').concat(a.nome)}" />
                                            <p:graphicImage styleClass="image grid_3" 
                                                            rendered="#{a.tipo eq 'application/pdf'}"
                                                            style="margin-left:1%; width: 40px;" 
                                                            url="assets/images/pdf.jpeg" />
                                            <div class="clear"/>

                                            <p:outputLabel value="#{managerCriarProtocolo.nomeAnexo(a)}" 
                                                           style="margin-left:-5px; width: 100px; font-size: 13px;" 
                                                           rendered="#{a.tipo ne 'application/pdf'}"/>
                                            <p:outputLabel  value="#{managerCriarProtocolo.nomeAnexoPdf(a)}" 
                                                            style="margin-left:-5px; width: 100px; font-size: 13px" 
                                                            rendered="#{a.tipo eq 'application/pdf'}"/>
                                        </div>
                                        <f:param name="annex" value="#{a.url.concat(a.nome)}"/>
                                    </h:outputLink>
                                </p:panel>
                            </ui:repeat>
                        </p:column> 

                    </p:dataTable>

                </p:panel>

                <div class="buttonAction">
                    <p:commandButton value="Salvar" actionListener="#{managerCriarProtocolo.salvar()}"
                                     icon="ui-icon-disk" styleClass="ui-priority-primary" 
                                     update="@form, @all" />
                    <p:button value="Cancelar" icon="ui-icon-circle-close"
                              href="indexprotocolo.xhtml" />
                </div>
            </h:form>

            <script  type="text/javascript" src="media/js/autoNumeric.js"></script>

            <script language="JavaScript" type="text/javascript">
                function required(rqd){
                    if(rqd){
                        $('#protocoloForm\\:processoCamposPanel .ui-outputlabel').each(function() {
                            var id = $(this).attr('for').replace(':', '\\:');
                            /* <![CDATA[ */
                            if ($(this).children('span').hasClass('required') && ($('#' + id).val() == '')) {
                                $(this).addClass('ui-state-error');
                                $('#' + id).addClass('ui-state-error');
                            }
                            /* ]]> */
                        });
                    }else{
                        $('#protocoloForm\\:processoCamposPanel').children().removeClass('ui-state-error');
                    }
                }

                function requiredCredor(rqd){
                    if(rqd){
                        $('#protocoloForm\\:clientePanel .ui-outputlabel').each(function() {
                            var id = $(this).attr('for').replace(':', '\\:');
                            /* <![CDATA[ */
                            if ($(this).children('span').hasClass('required') && ($('#' + id).val() == '')) {
                                $(this).addClass('ui-state-error');
                                $('#' + id).addClass('ui-state-error');
                            }
                            /* ]]> */
                        });
                    }else{
                        $('#protocoloForm\\:clientePanel').children().removeClass('ui-state-error');
                    }
                }
            </script>

        </ui:define>
    </ui:composition>
</html>


